const db=require('../db/index')
const bcrypt=require('bcryptjs')
exports.getUserInfo = (req, res) => {
    const sql='select id,username,nickname,email,user_pic from ev_user where id=?'
   db.query(sql,req.user.id,(err,results)=>{
       if(err)return res.cc(err)
       if(results.length!==1)return res.cc('获取用户信息失败')
       res.send({
           status:0,
           message:'获取用户信息成功',
           data:results[0]
       })
   })
}
exports.updateUserinfo=(req,res)=>{
    const sql='update ev_user set ? where id=?'
    db.query(sql,[req.body,req.body.id],(err,results)=>{
        if(err)return res.cc(err)
        if(results.affectedRows!==1) return res.cc('更新用户失败')
        res.cc('更新用户成功',0)
    })
}
exports.updatePassword=(req,res)=>{
    const sql='select * from ev_user where id=?'
db.query(sql,[req.user.id],(err,results)=>{
    if(err)return res.cc(err)
    if(results.length!==1)return res.cc('用户不存在')
    const compareResult=bcrypt.compareSync(req.body.oldPwd,results[0].password)
    if(!compareResult)return res.cc('原密码错误！')
    // res.send('ok')
    const sqlStr='update ev_user set password=? where id=?'
    const newPwd=bcrypt.hashSync(req.body.newPwd,10)
    db.query(sqlStr,[newPwd,req.user.id],(err,results)=>{
        if(err)return res.cc(err)
        if(results.affectedRows!==1)return res.cc('更新密码失败')
        res.cc('更新密码成功',0)
    })
})
}
exports.updateAvatar=(req,res)=>{
    const sql='update ev_user set user_pic=? where id=?'
    db.query(sql,[req.body.avatar,req.user.id],(err,results)=>{
        if(err)return res.cc(err)
        if(results.affectedRows!==1)return res.cc('更新头像失败')
        res.cc('更新头像成功',0)
    })
}

